package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.opportunityManager.OpportunityContact;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.OpportunityContactDbService;
import com.crm.system.service.opportunityManager.OpportunityContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 商机联系人
 */
@Service
public class OpportunityContactServiceImpl implements OpportunityContactService {

    @Autowired
    private OpportunityContactDbService opportunityContactDbService;

    /**
     * 商机-商机详情-查询当前商机联系人
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public DhPageInfo<OpportunityContact> opportunityContactList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String opportunityId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<OpportunityContact> list = opportunityContactDbService.opportunityContactList(opportunityId,"create_tm desc");
        PageInfo<OpportunityContact> pageInfo = new PageInfo<OpportunityContact>(list);
        return new DhPageInfo<OpportunityContact>(pageInfo, list);
    }

    /**
     * 商机管理-商机详情-新增联系人到商机联系人下
     * @param opportunityContact 商机联系人实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean addOpportunityContact(OpportunityContact opportunityContact) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        if(0 == opportunityContact.getContactType()){
            //查询同一个商机下联系人只能有一个首要联系人   需要商机id和联系类型
            List<OpportunityContact> list = opportunityContactDbService.getOpportunityAndContactType(opportunityContact.getOpportunityId(),opportunityContact.getContactType());
            if(list.size() !=0){
                OpportunityContact contact = list.get(0);
                //设置为次要联系人
                contact.setContactType(1);
                opportunityContactDbService.updateOpportunityContact(contact);
            }
        }
        String opportunityContactId = CommonUtils.getGUID();
        opportunityContact.setOpportunityContactId(opportunityContactId);
        opportunityContact.setCreatePeopleId(userAccount.getAccountId());
        opportunityContact.setCreateTm(new Date());
        opportunityContact.setUpdatePeopleId(userAccount.getAccountId());
        opportunityContact.setUpdateTm(new Date());
        opportunityContact.setAbolishState(1); //废止状态 启动
        opportunityContact.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        Boolean result = opportunityContactDbService.addOpportunityContact(opportunityContact);
        return result;
    }

    /**
     *客户详情--联系人 开启/废止联系人
     * @param opportunityContact 废止商机联系人实体
     * @return
     */
    @Override
    public Boolean updateAbolishStateById(OpportunityContact opportunityContact) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        opportunityContact.setUpdatePeopleId(userAccount.getAccountId());
        opportunityContact.setUpdateTm(new Date());
        Boolean result = opportunityContactDbService.updateOpportunityContact(opportunityContact);
        return result;
    }

    /**
     *商机详情--商机联系人-修改联系人类型-为首要联系人
     * @param opportunityContact 商机联系人实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean updateContactType(OpportunityContact opportunityContact) {
        //先把首要联系人  1  改成从联系人 2
        List<OpportunityContact> list = opportunityContactDbService.getOpportunityAndContactType(opportunityContact.getOpportunityId(),0);
        if(list.size() !=0){
            OpportunityContact contact = list.get(0);
            //设置为次要联系人
            contact.setContactType(1);
            opportunityContactDbService.updateOpportunityContact(contact);
        }

        //再把次要联系人修改为首要联系人
        Boolean result = opportunityContactDbService.updateOpportunityContact(opportunityContact);
        return result;
    }
}
